import store from '@/store/index.js';

export default {
	//页面跳转
	redirect(page, params = {}) {
		const queryStr = Object.entries(params).map(([k, v]) => `${k}=${v}`).join('')
		const url = `${page}?${queryStr}`
		uni.navigateTo({
			url
		})
	},
	//tabbar跳转
	switchTab(url) {
		uni.switchTab({
			url
		})
	},
	//预览图片
	previewImage(images) {
		uni.previewImage({
			urls: images
		})
	},
	//初始化用户数据
	async initUserData() {
		if (uni.getStorageSync('token')) {
			//加载当前用户
			store.dispatch('user/getCurrentUser')
			//获取用户微信信息
			store.dispatch('user/getUserWechatInfo')
			//加载用户地址
			store.dispatch('address/load')
		}
	},
	//登录检测
	isLogin() {
		return Boolean(uni.getStorageSync('token'))
	},
	//登录检测未登录用户自动跳转到登录碳
	loginCheck() {
		if (!this.isLogin()) {
			let route = getCurrentPages().pop().route;
			uni.setStorageSync('redirect_route', `/${route}`);
			uni.reLaunch({
				url: "/pages/login/login"
			})
			return false;
		}
		return true;
	},
}
